package com.housekeeping.housekeeping.serviceTest.controller.orders;/*
 * @author  pyh
 * @date  2021/1/22 0022 下午 3:45
 * @version 1.0
 * @since JDK1.8_241
 */

import com.housekeeping.housekeeping.order.entity.Order;
import com.housekeeping.housekeeping.order.service.impl.OrderSearchServImpl;
import com.housekeeping.housekeeping.order.utils.PageUtils;
import com.housekeeping.housekeeping.order.utils.ResponseCode;
import com.housekeeping.housekeeping.order.utils.ResponseEntity;
import com.housekeeping.housekeeping.order.utils.ResponseUtils;
import com.housekeeping.housekeeping.serviceTest.entity.Orders;
import com.housekeeping.housekeeping.serviceTest.service.impl.SearchTestServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/orders/statusAndPhone")
public class OrdersSearchByStatusAndPhone extends HttpServlet {
    private SearchTestServiceImpl orderSearchServ = new SearchTestServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        int status = Integer.parseInt(req.getParameter("status"));
        //分页需要的两个参数
        int page = Integer.parseInt(req.getParameter("page"));
        int size = Integer.parseInt(req.getParameter("size"));

        ResponseEntity<PageUtils<List<Orders>>> entity = null;

        try {
            if (phone == null || phone.equals("")) {
                PageUtils<List<Orders>> statusList = orderSearchServ.getStateList(status, page, size);
                entity = ResponseEntity.success(statusList);
            } else if (phone != null && !phone.equals("")) {
                PageUtils<List<Orders>> pAndSList = orderSearchServ.getPAndSList(phone, status, page, size);
                entity = ResponseEntity.success(pAndSList);
            } else {
                entity = ResponseEntity.error(ResponseCode.STATE_PHONE_SEARCH_ERROR);
            }
        } catch (SQLException throwables) {
            entity = ResponseEntity.error(ResponseCode.STATE_PHONE_SEARCH_ERROR);
        }

        ResponseUtils.deliver(resp, entity);
    }
}
